package com.example.cool_core.http;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public abstract class BaseHttpObserver<T> implements Observer<T> {
    private Object tag;


    public void setTag(Object tag) {
        this.tag = tag;
    }

    public BaseHttpObserver() {
    }

    public BaseHttpObserver(Object tag) {
        this.tag = tag;
    }

    @Override
    public void onComplete() {
        //正常结束
    }

    /**
     * 开始订阅 也就是开始网络请求
     *
     * @param d
     */
    @Override
    public void onSubscribe(Disposable d) {
        if (tag != null) {
            RxDisposableUtil.getInstance().addDisposable(tag, d);
        }
        HttpStart();
    }


    @Override
    public void onNext(T t) {
        HttpSuccess(t);
    }

    /**
     * 错误
     * 有可能是网络请求错误 比如接口解析的问题 等等
     * 也可能是配置问题
     *
     * @param e
     */
    @Override
    public void onError(Throwable e) {
        HttpError(e);
    }

    protected abstract void HttpStart();

    protected abstract void HttpError(Throwable e);

    protected abstract void HttpSuccess(T t);
}
